import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class myLinkIdentifier
{
	/**
	 * <a .. href
	 * <img .. src= 
	 * <form .. action
	 * <input .. src=
	 * <td .. background
	 * Look for anchor OR http:// OR /xyz
	 * for last case you need to add the base URL
	**/

	static String ptnHttp = "http:.+?(\"|\\s)";
	static String ptnHref = "href(\\s*?)=(\\s*?).+?(\")" ;
	static String ptnSrc = "(href|src)(\\s*?)=(\\s*?).+?(\")";
	static List<String> names= new ArrayList<String>();
	static String urlToClean;
	static String urlCleaned;
	static String urlBase;
	

	
	static
	{
		names.add(ptnHttp);
		names.add(ptnHref);
		names.add(ptnSrc);
	}


	
	public static Map extractURL(String line, String base)
	{
		String[] strArr = new String[Constants.LINKS_LIST_SIZE_GLOBAL];
		Map<Integer, String> linkMap = new HashMap<Integer, String>(Constants.LINKS_LIST_SIZE_GLOBAL);
		Map<Integer, myLink> linkMapUrl = new HashMap<Integer, myLink>(Constants.LINKS_LIST_SIZE_GLOBAL);
		
		Iterator<String> it=names.iterator();
		
		while(it.hasNext())
		{
			strArr = matchPattern((String)it.next(), line, base, 1);
		
			for(int strArrLength = strArr.length-1; strArrLength >= 0; strArrLength--)
			{
				//urlCleaned = CleanedURL(strArr[strArrLength], base, 1) ;
				//System.out.println("Length "+strArrLength);
				if(strArr[strArrLength] != null)
				{
					linkMap.put(new Integer(strArr[strArrLength].hashCode()), strArr[strArrLength]);
					//linkMapUrl.put(new Integer(strArr[strArrLength].hashCode()), 
							//new myLink(base, strArr[strArrLength]) );
				}
			}
			
			//strArr = matchPattern(ptnSrc, line);
			
			//linkMap.put(new Integer(matcher.group().hashCode()), matcher.group()); 
	        //System.out.println("No of elements found: "+linkMap.size());
		}
		return linkMap;	
	}

	protected static String[] matchPattern(String strPattern, String line, String base, int type)
	{
		String[] strArr = new String[Constants.LINKS_LIST_SIZE_GLOBAL];
		Pattern pattern = Pattern.compile(strPattern);
		Matcher matcher = pattern.matcher(line); // String 
		
        boolean DEBUG_MATCH_PATTERN = false;

        int i_loop = 0;
        while (matcher.find()) 
        {
        	urlCleaned = CleanedURL(matcher.group(), base, 1);
        	System.out.printf("I found the text |%s| starting at " +
               "index %d and ending at index %d.%n",
                urlCleaned, matcher.start(), matcher.end());
            
            strArr[i_loop] = urlCleaned;
            
            DEBUG_MATCH_PATTERN = true;
            i_loop = i_loop+1;
        }

        if(!DEBUG_MATCH_PATTERN)
        {
            System.out.println("No match found.");
        }

        return strArr;
	}
	
	protected static String CleanedURL(String toClean, String base, int type)
	{		
		String result = null;
		if(type == 1)
			result = toClean.substring(0, toClean.length()-1);
		
		return result;
	}
}
